package com.chunhe.sojourn.app;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.chunhe.sojourn.app.domain.LinkUrl;
import com.chunhe.sojourn.app.service.LinkUrlService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;

@Controller
@RequestMapping("/index")
@Slf4j
public class IndexController {
    @Resource
    private LinkUrlService linkUrlService;

    @GetMapping("/{channelCode}")
    @ApiOperation("获取服务师ID获取服务项列表")
    public String index(@PathVariable String channelCode) throws Exception {
        return getPage(channelCode);
    }
    @GetMapping("/page.html")
    @ApiOperation("获取服务师ID获取服务项列表")
    public String index1(String c) throws Exception {
        return getPage(c);
    }

    private String getPage(String channelCode){
        LambdaQueryWrapper<LinkUrl> wrapper = new LambdaQueryWrapper<LinkUrl>().eq(LinkUrl::getChannelCode, channelCode)
                .orderByDesc(LinkUrl::getSort).orderByAsc(LinkUrl::getId)
                .last("limit 1");
        wrapper.apply(" request_count < limit_count ");
        LinkUrl linkUrl = linkUrlService.getOne(wrapper);
        linkUrlService.update(new LambdaUpdateWrapper<LinkUrl>().set(LinkUrl::getRequestCount,linkUrl.getRequestCount()+1).eq(LinkUrl::getId,linkUrl.getId()));
        return "redirect:"+linkUrl.getLinkUrl();
    }
}
